package com.itany.shop.dao.impl;

import com.itany.shop.dao.ProductDao;
import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.rowmapper.ProductRowMapper;
import com.itany.shop.util.JDBCTemplate;

import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class ProductDaoImpl implements ProductDao {
    private JDBCTemplate jdbcTemplate = (JDBCTemplate) ObjectFactory.getObject("jdbc");

    @Override
    public List<Product> selectAll() {
        String sql = "select id,name,price from t_product";
        return jdbcTemplate.query(sql, new ProductRowMapper());
    }
    @Override
    public List<Product> selectPage(String pageNo, String pageSize) {
        int page = Integer.parseInt(pageNo);
        int size = Integer.parseInt(pageSize);
        String sql = "select id,name,price from t_product limit ?, ?";
        return jdbcTemplate.query(sql, new ProductRowMapper(), (page - 1) * size, size);
    }
    @Override
    public Product selectById(Integer id) {
        String sql = "select id,name,price from t_product where id = ?";
        List<Product> products = jdbcTemplate.query(sql, new ProductRowMapper(), id);
        return products.isEmpty() ? null : products.get(0);
    }
}
